package at.ac.tuwien.ifs.calendit.basis.gui;

import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import at.ac.tuwien.ifs.calendit.basis.helper.Constants;
import at.ac.tuwien.ifs.calendit.dao.IAbteilung;
import at.ac.tuwien.ifs.calendit.dao.IMitarbeiter;
import at.ac.tuwien.ifs.calendit.domain.Abteilung;
import at.ac.tuwien.ifs.calendit.domain.Mitarbeiter;

public class AbteilungsverwaltungTableModel extends AbstractTableModel {

	/**
	 * serialVersionUID, generated by eclipse.
	 */
	private static final long serialVersionUID = 7400901757402840405L;

	/**
	 * Retrieves the logger for this class.
	 */
	private static Log log = LogFactory.getLog(AbteilungsverwaltungTableModel.class);

	/**
	 * The Data Access Object for Abteilungen.
	 */
	private IAbteilung abteilungDAO = null;


	/**
	 * The Data Access Object for Mitarbeiter.
	 */
	private IMitarbeiter maDAO = null;
	
	/**
	 * Spring Framework XML Bean Factory.
	 */
	private XmlBeanFactory xbf;

	/**
	 * Defines the Names of the Columns of the TableModel.
	 */
	private final String[] columnName = { "bezeichnung", "abteilungsleiter" };

	/**
	 * The list of Abteilungen will be filled by abteilungDAO.
	 */
	private List<Abteilung> abt = null;

	/**
	 * Creates a new AbteilungTableModel with default ordering.
	 * 
	 * @param order
	 *            the default Order
	 * @see #order
	 */
	public AbteilungsverwaltungTableModel(String order) {
		super();
		ClassPathResource res = new ClassPathResource(Constants.SPRINGBEANS);
		xbf = new XmlBeanFactory(res);
		abteilungDAO = (IAbteilung) xbf.getBean("AbteilungDAO");
		maDAO = (IMitarbeiter) xbf.getBean("MitarbeiterDAO");
		readData();
	}

	/**
	 * Get a List of Abteilungen represented in this TableModel.
	 * 
	 * @return the Abteilungen represented in the TableModel
	 * @see Abteilung
	 * @see List
	 */
	public List<Abteilung> getAbteilungen() {
		return abt;
	}

	/**
	 * Returns a single Abteilung out of this TableModel.
	 * 
	 * @param bez
	 *            The bezeichnung of the desired Abteilung.
	 * @return The Abteilung at position <c>i</c>.
	 * @see #getAbteilungen()
	 */
	public Abteilung getAbteilungAt(int i) {
		return abt.get(i);
	}

	/**
	 * Read the Abteilung from the Database with given order.
	 * 
	 * @see MitarbeiterDAO#getMitarbeiters(String)
	 */
	private void readData() {
		abt = abteilungDAO.getAbteilungen();
	}

	/**
	 * Reload the Data.
	 */
	public void reload() {
		readData();
		fireTableDataChanged();
	}

	/**
	 * Change the Order and reload the Data.
	 * 
	 * @param order
	 *            the new Order
	 * @see #order
	 */
	public void setOrder(String order) {
		log.info("Order Changed: reading data...");
		reload();
	}

	/**
	 * Get the number of rows in this TableModel.
	 * 
	 * @return number of rows
	 */
	public int getRowCount() {
		log.debug("rowcount = " + abt.size());
		return abt.size();
	}

	/**
	 * Get the number of columns in this TableModel.
	 * 
	 * @return number of colums
	 */
	public int getColumnCount() {
		log.debug("colCount = " + columnName.length);
		return columnName.length;
	}

	/**
	 * Get the title of a column.
	 * 
	 * @param c
	 *            the column-index
	 * @return the title
	 */
	public String getColumnName(int c) {
		log.debug("colName = " + columnName[c]);
		return columnName[c];
	}

	/**
	 * Get the Class of the data saved in a colum.
	 * 
	 * @param c
	 *            the column-index
	 * @return String.class
	 */
	public Class<?> getColumnClass(int c) {
		return String.class;
	}

	/**
	 * Checks whether a cell is editable or not.
	 * 
	 * @param row
	 *            the Row
	 * @param col
	 *            the Column
	 * @return false in any case.
	 */
	public boolean isCellEditable(int row, int col) {
		return false;
	}

	/**
	 * Get the Value from a given Cell, denoted by row and col.
	 * 
	 * @param row
	 *            the Row
	 * @param col
	 *            the Column
	 * @return String containing the value of the cell.
	 */
	public Object getValueAt(int row, int col) {
		Abteilung a = abt.get(row);
		Mitarbeiter m = maDAO.getMitarbeiterById(a.getAbteilungsleiter());
		
		switch (col) {
		case 0:
			return "" + a.getBezeichnung();
		case 1:
			if (m!=null) 
				return m.getVname() + " "+ m.getNname();
			else //sollte eigentlich nie sein. eine abteilung hat immer abt-leiter
				return a.getAbteilungsleiter();
		default:
			return null;
		}
	}
}
